﻿using MvvmCross.Core.ViewModels;
using Sidvall.FileSystemManager.Data;
using Sidvall.FileSystemManager.UI.Entities;
using Sidvall.Serialization;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Sidvall.FileSystemManager.UI.ViewModels
{
    public partial class RenameConfigViewModel
    {
        #region OldPathNames1

        private PathNameCollection _OldPathNames1;
        public PathNameCollection OldPathNames1
        {
            get
            {
                PathNameCollection items;

                if (_OldPathNames1 == null)
                {
                    items = new PathNameCollection();
                    items.Add(1, "XXXXX");
                    items.Add(2, "{n}");
                    _OldPathNames1 = items;
                }
                return _OldPathNames1;
            }
        }

        #endregion
        #region OldPathNames2

        private PathNameCollection _OldPathNames2;
        public PathNameCollection OldPathNames2
        {
            get
            {
                PathNameCollection items;

                if (_OldPathNames2 == null)
                {
                    items = new PathNameCollection();
                    items.Add(1, "YYYYY");
                    _OldPathNames2 = items;
                }
                return _OldPathNames2;
            }
        }

        #endregion
        #region OldPathNames3

        private PathNameCollection _OldPathNames3;
        public PathNameCollection OldPathNames3
        {
            get
            {
                PathNameCollection items;

                if (_OldPathNames3 == null)
                {
                    items = new PathNameCollection();
                    _OldPathNames3 = items;
                }
                return _OldPathNames3;
            }
        }

        #endregion
        #region NewPathNames1

        private PathNameCollection _NewPathNames1;
        public PathNameCollection NewPathNames1
        {
            get
            {
                PathNameCollection items;

                if (_NewPathNames1 == null)
                {
                    items = new PathNameCollection();
                    items.Add(1, "Company.Product");
                    _NewPathNames1 = items;
                }
                return _NewPathNames1;
            }
        }

        #endregion
        #region NewPathNames2

        private PathNameCollection _NewPathNames2;
        public PathNameCollection NewPathNames2
        {
            get
            {
                PathNameCollection items;

                if (_NewPathNames2 == null)
                {
                    items = new PathNameCollection();
                    items.Add(1, "Product");
                    _NewPathNames2 = items;
                }
                return _NewPathNames2;
            }
        }

        #endregion
        #region NewPathNames3

        private PathNameCollection _NewPathNames3;
        public PathNameCollection NewPathNames3
        {
            get
            {
                PathNameCollection items;

                if (_NewPathNames3 == null)
                {
                    items = new PathNameCollection();
                    _NewPathNames3 = items;
                }
                return _NewPathNames3;
            }
        }

        #endregion
        #region SearchPatterns

        private PathNameCollection _SearchPatterns;
        public PathNameCollection SearchPatterns
        {
            get
            {
                PathNameCollection items;

                if (_SearchPatterns == null)
                {
                    items = new PathNameCollection();
                    items.Add(1, CodeManager.SearchPatterns);
                    items.Add(2, CodeManager.SearchPatternsCodeFiles);
                    items.Add(3, CodeManager.SearchPatternsProjects);
                    _SearchPatterns = items;
                }
                return _SearchPatterns;
            }
        }

        #endregion

        //Commands
        #region Rename

        public ICommand RenameCommand
        {
            get
            {
                return new MvxCommand(async () => await RenameAsync());
            }
        }
        public async System.Threading.Tasks.Task RenameAsync()
        {
            Task task = null;
            string[] searchPatterns;

            if (this.ActiveEntity == null)
                return;
            using (var m = new Sidvall.Data.BusyManager(this))
            {
                try
                {
                    var nameItems = new NameItemCollection();
                    if (!string.IsNullOrWhiteSpace(this.ActiveEntity.SearchPatterns))
                        searchPatterns = this.ActiveEntity.SearchPatterns.Split(CodeManager.SearchPatternSeparator);
                    else
                        searchPatterns = null;
                    nameItems.Add(this.ActiveEntity.OldName1, this.ActiveEntity.NewName1);
                    nameItems.Add(this.ActiveEntity.OldName2, this.ActiveEntity.NewName2);
                    nameItems.Add(this.ActiveEntity.OldName3, this.ActiveEntity.NewName3);
                    var result = await Sidvall.FileSystemManager.CodeManager.RenameAsync(this.ActiveEntity.FolderPath, nameItems, searchPatterns,
                        this.ActiveEntity.RenameFolders, this.ActiveEntity.RenameFiles, this.ActiveEntity.UpdateFileContents, this.ActiveEntity.CaseSensitive, this.ActiveEntity.RecursiveFolders);
                    if (!string.IsNullOrWhiteSpace(this.ActiveEntity.FolderPath))
                    {
                        SystemContext.Current.UserConfiguration.RenameConfigContext = this.ActiveEntity.Serialize();
                        await SystemContext.Current.SaveUserConfigurationAsync(SystemContext.Current.UserConfiguration);
                    }
                    await Sidvall.UI.SystemContext.Current.DialogManager.ShowMessageAsync("Result", result.GetDescription(), Sidvall.UI.Dialogs.MessageType.Information);
                }
                catch (System.Exception ex)
                {
                    task = Sidvall.UI.SystemContext.Current.DialogManager.ShowErrorMessageAsync("Unexpected Error", ex);
                }
                if (task != null)
                    await task;
            }
        }

        #endregion
        #region SetRenameNumber

        public ICommand SetRenameNumberCommand
        {
            get
            {
                return new MvxCommand(SetRenameNumber);
            }
        }
        public void SetRenameNumber()
        {
            this.ActiveEntity.OldName1 = "{n}";
            this.ActiveEntity.OldName2 = "";
            this.ActiveEntity.OldName3 = "";
            this.ActiveEntity.NewName1 = "2000";
            this.ActiveEntity.NewName2 = "";
            this.ActiveEntity.NewName3 = "";
            this.ActiveEntity.SearchPatterns = "";
            this.ActiveEntity.RenameFolders = true;
            this.ActiveEntity.RecursiveFolders = true;
            this.ActiveEntity.RenameFiles = false;
            this.ActiveEntity.UpdateFileContents = false;
            this.ActiveEntity.CaseSensitive = false;
        }

        #endregion
        #region SetRename

        public ICommand SetRenameCommand
        {
            get
            {
                return new MvxCommand(SetRename);
            }
        }
        public void SetRename()
        {
            this.ActiveEntity.OldName1 = "XXXXX";
            this.ActiveEntity.OldName2 = "YYYYY";
            this.ActiveEntity.OldName3 = "";
            this.ActiveEntity.NewName1 = "Company.Product";
            this.ActiveEntity.NewName2 = "Product";
            this.ActiveEntity.NewName3 = "";
            this.ActiveEntity.SearchPatterns = CodeManager.SearchPatterns;
            this.ActiveEntity.RenameFolders = true;
            this.ActiveEntity.RecursiveFolders = true;
            this.ActiveEntity.RenameFiles = true;
            this.ActiveEntity.UpdateFileContents = true;
            this.ActiveEntity.CaseSensitive = true;
        }

        #endregion

        #region LoadItemAsync

        protected override Task<RenameConfig> LoadItemAsync()
        {
            var item = RenameConfig.Deserialize(SystemContext.Current.UserConfiguration.RenameConfigContext);
            return Task.FromResult(item);
        }

        #endregion
        #region OnViewEventAsync

        protected override Task OnViewEventAsync(Sidvall.UI.Views.ViewEventArgs args)
        {
            return Task.FromResult(true);
        }

        #endregion

        #region StartAsync

        protected override async Task StartAsync()
        {
            SystemContext.Current.UserConfiguration.StartPageId = Data.PageType.Rename;
            await SystemContext.Current.SaveUserConfigurationAsync(SystemContext.Current.UserConfiguration);
            await DoLoadAsync();
        }

        #endregion
    }
}
